# -*- encoding=utf8 -*-

__author__ = "code哥"
import sys
import os
import subprocess
from sanic_openapi import swagger_blueprint
from api.render_index import blueprint as render_blueprint
from api.websocket_img import blueprint as ws_img_blueprint
from api.websocket_exec_curr import blueprint as ws_exec_curr_blueprint
from api.websocket_exec_health import blueprint as ws_exec_health_blueprint
from api.websocket_run_log import blueprint as ws_run_log_blueprint
from api.app_list import blueprint as app_list_blueprint
from api.app_service_host import blueprint as app_service_host_blueprint
from api.devices_list import blueprint as devices_list_blueprint
from api.app_device_img_list import blueprint as app_device_img_list_blueprint
from api.app_execution_time import blueprint as app_execution_time_blueprint
from api.next_day_execution_time import blueprint as next_day_execution_time_blueprint
from api.rym_start import blueprint as rym_start_blueprint
from api.rym_single_restart import blueprint as rym_single_restart_blueprint
from api.rym_stop import blueprint as rym_stop_blueprint
from api.rym_single_stop import blueprint as rym_single_stop_blueprint
from api.reg_wifi_devices import blueprint as reg_wifi_devices_blueprint
from sanic import Sanic
from sanic.websocket import WebSocketProtocol

curPath = os.path.abspath(os.path.dirname(__file__))
rootPath = os.path.split(curPath)[0]
sys.path.append(rootPath)

app = Sanic(__name__)
app.static('/static', './static')

app.blueprint(swagger_blueprint)
app.blueprint(render_blueprint)
app.blueprint(app_list_blueprint)
app.blueprint(app_service_host_blueprint)
app.blueprint(devices_list_blueprint)
app.blueprint(next_day_execution_time_blueprint)
app.blueprint(app_device_img_list_blueprint)
app.blueprint(app_execution_time_blueprint)
app.blueprint(ws_img_blueprint)
app.blueprint(ws_exec_curr_blueprint)
app.blueprint(ws_exec_health_blueprint)
app.blueprint(ws_run_log_blueprint)
app.blueprint(rym_start_blueprint)
app.blueprint(rym_single_restart_blueprint)
app.blueprint(rym_stop_blueprint)
app.blueprint(rym_single_stop_blueprint)
app.blueprint(reg_wifi_devices_blueprint)


@app.listener('before_server_start')
async def before_server_start(app, loop):
    print('before_server_start')
    await popen_wait("ps -ef | grep watch_dir.py | grep -v grep | awk '{print $2}' | xargs kill -TERM")
    await popen_wait("ps -ef | grep tail_log.py | grep -v grep | awk '{print $2}' | xargs kill -TERM")
    await popen_no_wait('nohup python3 -u {path}/watch_dir.py >> {path}/run.log 2>&1 &'.format(
        path=os.path.dirname(os.path.abspath(__file__))))
    await popen_no_wait('nohup python3 -u {path}/tail_log.py >> {path}/run.log 2>&1 &'.format(
        path=os.path.dirname(os.path.abspath(__file__))))
    await popen_no_wait("ps -ef | grep start.py | grep -v grep | awk '{print $2}' | xargs kill -TERM")


@app.listener('after_server_start')
async def after_server_start(app, loop):
    print('after_server_start')


@app.listener('before_server_stop')
async def before_server_stop(app, loop):
    print('before_server_stop')
    await popen_no_wait("ps -ef | grep watch_dir.py | grep -v grep | awk '{print $2}' | xargs kill -TERM")
    await popen_no_wait("ps -ef | grep tail_log.py | grep -v grep | awk '{print $2}' | xargs kill -TERM")
    await popen_no_wait("ps -ef | grep start.py | grep -v grep | awk '{print $2}' | xargs kill -TERM")
    await popen_wait('nohup python3 -u {path}/stop.py >> {path}/run.log 2>&1 &'.format(
        path=os.path.dirname(os.path.abspath(__file__))))
    await popen_no_wait("ps -ef | grep stop.py | grep -v grep | awk '{print $2}' | xargs kill -TERM")


@app.listener('after_server_stop')
async def after_server_stop(app, loop):
    print('after_server_stop')


async def popen_wait(command):
    process = subprocess.Popen(command, shell=True, close_fds=True, stdin=subprocess.PIPE,
                               stdout=subprocess.PIPE,
                               stderr=subprocess.PIPE)
    process.wait()


async def popen_no_wait(command):
    subprocess.Popen(command, shell=True)


def main():
    app.run(host="0.0.0.0", port=9990, workers=1, protocol=WebSocketProtocol, debug=False)


if __name__ == '__main__':
    main()
